package com.domcer.ultra.menu.engine;

import com.domcer.ultra.menu.BootStrap;
import com.domcer.ultra.menu.application.cache.PlayerCache;
import com.domcer.ultra.menu.application.cache.TaskCache;
import com.domcer.ultra.menu.domain.MenuDomain;
import com.domcer.ultra.menu.domain.MenuItemDomain;
import com.domcer.ultra.menu.domain.MenuSettingsDomain;
import common.utils.MessageUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author hang.wang
 * @Date 2025/1/31 00:32
 */
public class MenuManager {

    /**
     * unified menu management center <br>
     * attention: all menu open must use this method
     * @param player {@link Player}
     * @param menuDomain {@link MenuDomain} which should get by upper level business
     */
    public static void open(Player player, MenuDomain menuDomain) {
        if (menuDomain == null) {
            return;
        }

        MenuSettingsDomain settings = menuDomain.getMenuSettings();
        boolean require = RequireManager.execute(player, menuDomain.getMenuSettings().getOpenRequires());

        if (!require) {
            return;
        }

        Map<String, MenuItemDomain> items = menuDomain.getItems();
        Inventory inventory = Bukkit.createInventory(
                null,
                settings.getRows() * 9,
                MessageUtil.exchangeMessage(settings.getName(), player)
        );
        supplementItems(player, inventory, settings, items);
        PlayerCache.setMenuDomain(player, menuDomain);
        player.openInventory(inventory);
        ActionManager.execute(player, settings.getOpenActions());
    }

    private static void supplementItems(Player player, Inventory inventory, MenuSettingsDomain settings, Map<String, MenuItemDomain> items) {
        items.forEach((itemKey, menuItemDomain) -> {
            Material material = Material.getMaterial(menuItemDomain.getMaterial().toUpperCase());
            if (material == null) {
                return;
            }

            boolean require = RequireManager.execute(player, menuItemDomain.getShowRequires());
            if (!require) {
                return;
            }

            ItemStack itemStack = new ItemStack(material, 1);
            ItemMeta itemMeta = itemStack.getItemMeta();
            assert itemMeta != null;
            itemMeta.setDisplayName(MessageUtil.exchangeMessage(menuItemDomain.getName().get(0), player));

            AtomicInteger index = new AtomicInteger(1);
            int slot = menuItemDomain.getSlot();
            BukkitTask bukkitTask = new BukkitRunnable() {
                @Override
                public void run() {
                    int i = index.incrementAndGet();
                    List<String> nameList = menuItemDomain.getName();
                    if (i >= nameList.size()) {
                        index.set(0);
                    }
                    itemMeta.setDisplayName(MessageUtil.exchangeMessage(nameList.get(index.get()), player));
                    itemStack.setItemMeta(itemMeta);
                    inventory.setItem(slot, itemStack);
                }
            }.runTaskTimer(BootStrap.PLUGIN, 0L, 10L);
            TaskCache.addTask(player, bukkitTask);

            itemStack.setItemMeta(itemMeta);

            inventory.setItem(slot, itemStack);
        });
    }
}
